package com.jeecms.wicool.action.user;

import static com.jeecms.common.page.SimplePage.cpn;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;

import com.jeecms.cms.entity.main.CmsUser;
import com.jeecms.cms.manager.main.CmsLogMng;
import com.jeecms.cms.web.CmsUtils;
import com.jeecms.cms.web.WebErrors;
import com.jeecms.common.page.Pagination;
import com.jeecms.common.web.CookieUtils;
import com.jeecms.wicool.entity.product.Product;
import com.jeecms.wicool.entity.user.ScoreTrade;
import com.jeecms.wicool.entity.user.UserInfo;
import com.jeecms.wicool.entity.util.Dictionary2;
import com.jeecms.wicool.manager.user.ScoreTradeMng;
import com.jeecms.wicool.manager.user.UserInfoMng;
import com.jeecms.wicool.manager.util.DictionaryMng;

/**
 * 会员卡Action
 */
@Controller
public class ScoreTradeAct {
	private static final Logger log = LoggerFactory.getLogger(ScoreTradeAct.class);

	@RequestMapping("/user/v_listScoreTrade.do")
	public String list(Integer userId,String userName,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(userId,userName,cpn(pageNo),CookieUtils.getPageSize(request));
		List<ScoreTrade> list = (List<ScoreTrade>) pagination.getList();
		for(ScoreTrade s :list){
			if(s.getTradeSource()!=null){
			s.setTradeSourceName(dicMng.findByDicCode("trade_source", Short.parseShort(s.getTradeSource().toString())).getDicName());
			}
			s.setTradeTypeName(dicMng.findByDicCode("coupon_type", Short.parseShort(s.getTradeType().toString())).getDicName());
		}
		 model.addAttribute("pagination", pagination);
		model.addAttribute("userName",userName);
		return "user/list_ScoreTrade";
	}
	@RequestMapping("/user/v_viewListScoreTrade.do")
	public String viewListScoreTrade(Integer userId,String userName,Integer pageNo,HttpServletRequest request, ModelMap model) {
		Pagination pagination = manager.getPage(userId,userName,cpn(pageNo),CookieUtils.getPageSize(request));
		List<ScoreTrade> list = (List<ScoreTrade>) pagination.getList();
		for(ScoreTrade s :list){
			if(s.getTradeSource()!=null){
			s.setTradeSourceName(dicMng.findByDicCode("trade_source", Short.parseShort(s.getTradeSource().toString())).getDicName());
			}
			s.setTradeTypeName(dicMng.findByDicCode("coupon_type", Short.parseShort(s.getTradeType().toString())).getDicName());
		}
		 model.addAttribute("pagination", pagination);
		model.addAttribute("userName",userName);
		return "user/view_ListScoreTrade";
	}
	@RequestMapping("/user/v_addScoreTrade.do")
	public String add(ModelMap model,HttpServletRequest request) {
		
		List<UserInfo> userList = userMng.getAll(CmsUtils.getUser(request));
		List<Dictionary2> tradeTypeList = dicMng.getList("coupon_type");
		List<Dictionary2> tradeSourceList = dicMng.getList("trade_source");
		
		model.addAttribute("userList",userList);
		model.addAttribute("tradeTypeList",tradeTypeList);
		model.addAttribute("tradeSourceList",tradeSourceList);
		return "user/add_ScoreTrade";
	}
	
	@RequestMapping("/user/v_editScoreTrade.do")
	public String edit(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ScoreTrade ScoreTrade = manager.findById(id);
		List<UserInfo> userList = userMng.getAll(CmsUtils.getUser(request));
		List<Dictionary2> tradeTypeList = dicMng.getList("coupon_type");
		List<Dictionary2> tradeSourceList = dicMng.getList("trade_source");
		model.addAttribute("ScoreTrade",ScoreTrade);
		model.addAttribute("userList",userList);
		model.addAttribute("tradeTypeList",tradeTypeList);
		model.addAttribute("tradeSourceList",tradeSourceList);
		return "user/edit_ScoreTrade";
	}
	
	@RequestMapping("/user/v_viewScoreTrade.do")
	public String view(Integer id, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateEdit(id, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ScoreTrade ScoreTrade = manager.findById(id);
		model.addAttribute("ScoreTrade",ScoreTrade);
		return "user/view_ScoreTrade";
	}
	
	@RequestMapping("/user/o_saveScoreTrade.do")
	public String save(ScoreTrade bean,Integer userId, HttpServletRequest request, ModelMap model) {
		WebErrors errors = validateSave(bean, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean = manager.save(bean,userId);
		log.info("save ScoreTrade id={}", bean.getId());
		cmsLogMng.operating(request, "ScoreTrade.log.save",  "id=" + bean.getId());
		return "redirect:v_listScoreTrade.do";
	}
	@RequestMapping("/user/o_updateScoreTrade.do")
	public String update(ScoreTrade bean,Integer userId,Integer pageNo,HttpServletRequest request,ModelMap model) {
		WebErrors errors = validateUpdate(bean.getId(), request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		bean=manager.update(bean,userId);
		model.addAttribute("message", "global.success");
		log.info("update ScoreTrade success. id={}", bean.getId());
		cmsLogMng.operating(request, "ScoreTrade.log.update",  "id=" + bean.getId());
		return edit(bean.getId(), request, model);
	}
	
	@RequestMapping("/user/o_deleteScoreTrade.do")
	public String delete(Integer[] ids, String userName,Integer pageNo, HttpServletRequest request,
			ModelMap model) {
		WebErrors errors = validateDelete(ids, request);
		if (errors.hasErrors()) {
			return errors.showErrorPage(model);
		}
		ScoreTrade[] beans = manager.deleteByIds(ids);
		for (ScoreTrade bean : beans) {
			log.info("delete ScoreTrade id={}", bean.getId());
			cmsLogMng.operating(request, "ScoreTrade.log.delete", "id="
					+ bean.getId() + ";name=" + bean.getUser().getName());
		}
		return list( null,userName, pageNo, request, model);
	}
	
	private WebErrors validateSave( ScoreTrade bean, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		return errors;
	}

	private WebErrors validateEdit(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		return errors;
	}

	private WebErrors validateUpdate(Integer id, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		if (vldExist(id, errors)) {
			return errors;
		}
		// TODO 验证是否为管理员，管理员不允许修改。
		return errors;
	}
	private WebErrors validateDelete(Integer[] ids, HttpServletRequest request) {
		WebErrors errors = WebErrors.create(request);
		errors.ifEmpty(ids, "ids");
		for (Integer id : ids) {
			vldExist(id, errors);
		}
		return errors;
	}

	private boolean vldExist(Integer id, WebErrors errors) {
		if (errors.ifNull(id, "id")) {
			return true;
		}
		ScoreTrade entity = manager.findById(id);
		if (errors.ifNotExist(entity, Product.class, id)) {
			return true;
		}
		return false;
	}
	@Autowired
	private ScoreTradeMng manager;
	@Autowired
	private CmsLogMng cmsLogMng;
	@Autowired
	private UserInfoMng userMng;
	@Autowired
	private DictionaryMng dicMng;
}
